<script setup lang="ts">
import { ref, watch, computed } from 'vue';

import { message } from 'ant-design-vue';

interface InpatientGroup {
  id: string;
  petId: string;
  admissionDate: any;
  conditionClassification: string;
  responsibleDoctor: string;
  dischargeMethod: 'manual' | 'specified';
  dischargeDate?: any;
  stayDuration: number;
  remarks: string;
  status: 'active' | 'discharged';
  createTime: string;
}

interface Props {
  visible: boolean;
  inpatientGroup?: InpatientGroup | null;
}

interface Emits {
  (e: 'update:visible',visible: boolean): void;
  (e: 'success',data: any): void
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

// 表单数据
const formData = ref({
  itemName: '',
  unitPrice: 0,
  quantity: 1,
  description: '',
});

// 预设的固定消费项目
const presetItems = [
  { name: '住院床位费', price: 80, description: '每日住院床位费用' },
  { name: '护理费', price: 50, description: '每日基础护理费用' },
  { name: '药品费', price: 30, description: '每日基础药品费用' },
  { name: '检查费', price: 100, description: '每日基础检查费用' },
  { name: '治疗费', price: 120, description: '每日基础治疗费用' },
  { name: '营养费', price: 40, description: '每日营养补充费用' },
  { name: '清洁费', price: 20, description: '每日清洁消毒费用' },
  { name: '管理费', price: 25, description: '每日管理服务费用' },
];

// 已选择的固定消费项目
const selectedItems = ref<any[]>([]);

// 重置表单
const resetForm = () => {
  formData.value = {
    itemName: '',
    unitPrice: 0,
    quantity: 1,
    description: '',
  };
};

// 监听props变化
watch(
  () => props.visible,
  (visible) => {
    if (visible) {
      resetForm();
    }
  },
);

// 取消
const handleCancel = () => {
  emit('update:visible', false);
  resetForm();
};

// 添加预设项目
const addPresetItem = (item: any) => {
  const existingItem = selectedItems.value.find((i) => i.name === item.name);
  if (existingItem) {
    message.warning('该项目已添加');
    return;
  }

  selectedItems.value.push({
    ...item,
    id: Date.now().toString(),
    quantity: 1,
  });
};

// 移除项目
const removeItem = (itemId: string) => {
  selectedItems.value = selectedItems.value.filter(
    (item) => item.id !== itemId,
  );
};

// 计算总消费
const totalDailyConsumption = computed(() => {
  return selectedItems.value.reduce((total, item) => {
    return total + item.price * item.quantity;
  }, 0);
});

// 确定
const handleOk = () => {
  if (selectedItems.value.length === 0) {
    message.warning('请至少选择一个固定消费项目');
    return;
  }

  // 构建固定消费数据
  const fixedConsumptionData = {
    id: Date.now().toString(),
    items: selectedItems.value,
    totalDailyAmount: totalDailyConsumption.value,
    description: '每日固定消费项目',
  };

  // 发送数据给父组件
  emit('success', fixedConsumptionData);
  message.success('固定消费设置成功！');
  emit('update:visible', false);
  resetForm();
  selectedItems.value = [];
};
</script>

<template>
  <a-modal
    :visible="visible"
    title="设置每日固定消费"
    width="800px"
    @cancel="handleCancel"
    @ok="handleOk"
  >
    <div class="fixed-consumption-modal">
      <!-- 预设项目选择 -->
      <div class="preset-items-section">
        <h4>选择预设项目</h4>
        <div class="preset-items-grid">
          <div
            v-for="item in presetItems"
            :key="item.name"
            class="preset-item-card"
            @click="addPresetItem(item)"
          >
            <div class="item-name">{{ item.name }}</div>
            <div class="item-price">¥{{ item.price }}</div>
            <div class="item-description">{{ item.description }}</div>
          </div>
        </div>
      </div>

      <!-- 已选择的项目 -->
      <div class="selected-items-section">
        <h4>已选择的项目</h4>
        <div v-if="selectedItems.length === 0" class="no-items">
          暂无选择的项目
        </div>
        <div v-else class="selected-items-list">
          <div
            v-for="item in selectedItems"
            :key="item.id"
            class="selected-item"
          >
            <div class="item-info">
              <span class="item-name">{{ item.name }}</span>
              <span class="item-price">¥{{ item.price }}</span>
            </div>
            <div class="item-actions">
              <a-input-number
                v-model:value="item.quantity"
                :min="1"
                :max="10"
                size="small"
                style="width: 80px"
              />
              <a-button
                type="text"
                size="small"
                danger
                @click="removeItem(item.id)"
              >
                删除
              </a-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 总消费金额 -->
      <div class="total-section">
        <div class="total-amount">
          <span class="total-label">每日总消费：</span>
          <span class="total-value"
            >¥{{ totalDailyConsumption.toFixed(2) }}</span
          >
        </div>
      </div>
    </div>
  </a-modal>
</template>

<style scoped>
.fixed-consumption-modal {
  padding: 16px 0;
}

.preset-items-section,
.selected-items-section {
  margin-bottom: 24px;
}

.preset-items-section h4,
.selected-items-section h4 {
  margin-bottom: 16px;
  font-size: 16px;
  color: #333;
}

.preset-items-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}

.preset-item-card {
  padding: 12px;
  cursor: pointer;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  transition: all 0.3s;
}

.preset-item-card:hover {
  background: #f0f8ff;
  border-color: #1890ff;
}

.preset-item-card .item-name {
  margin-bottom: 4px;
  font-weight: 500;
  color: #333;
}

.preset-item-card .item-price {
  margin-bottom: 4px;
  font-size: 16px;
  font-weight: 600;
  color: #1890ff;
}

.preset-item-card .item-description {
  font-size: 12px;
  color: #666;
}

.no-items {
  padding: 20px;
  color: #999;
  text-align: center;
  background: #fafafa;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}

.selected-items-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.selected-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  background: #fafafa;
  border: 1px solid #f0f0f0;
  border-radius: 6px;
}

.item-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.item-info .item-name {
  font-weight: 500;
  color: #333;
}

.item-info .item-price {
  font-size: 14px;
  color: #1890ff;
}

.item-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.total-section {
  padding: 16px;
  background: #f6ffed;
  border: 1px solid #b7eb8f;
  border-radius: 6px;
}

.total-amount {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.total-label {
  font-size: 16px;
  color: #333;
}

.total-value {
  font-size: 20px;
  font-weight: 600;
  color: #52c41a;
}
</style>
